using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

namespace Sucrose.Base
{
    public static class Util
    {
        public static String GetFilenameFromPath(String path)
        {
            int lastBackSlash = path.LastIndexOf('\\');
            int lastFwdSlash  = path.LastIndexOf('/');

            String filename = "";

            if (lastBackSlash != -1)
                filename = path.Substring(lastBackSlash + 1, path.Length - lastBackSlash - 1);
            else if (lastFwdSlash != -1)
                filename = path.Substring(lastFwdSlash + 1, path.Length - lastFwdSlash - 1);

            return filename;
        }

        public static int getEnumeratorCount(IEnumerator enumerator)
        {
            int count = 0;

            while (enumerator.MoveNext())
            {
                ++count;
            }

            enumerator.Reset();
            return count;
        }

        public static bool CheckNetworkConnection()
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in interfaces)
            {
                if(adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && adapter.OperationalStatus == OperationalStatus.Up)
                    return true;
            }
            return false;
        }

        public static bool CheckInternetConnection()
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();

            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            PingReply reply = pingSender.Send("google.com", 1000, buffer, options);
            if (reply.Status == IPStatus.Success)
                return true;
            else
                return false;
        }

        public static int GetLetterCount(string s, char letter)
        {
            int num = 0;

            for (int x = 0; x < s.Length; x++)
            {
                if (s[x] == letter)
                    num++;
            }

            return num;
        }

        private static int IntArrayMin(int[] arr)
        {
            int min = Int32.MaxValue;

            for (int x = 0; x < arr.Length; x++)
            {
                if (arr[x] < min)
                    min = arr[x];
            }

            return min;
        }

        private static int IntArrayMax(int[] arr)
        {
            int max = 0;

            for (int x = 0; x < arr.Length; x++)
            {
                if (arr[x] > max)
                    max = arr[x];
            }

            return max;
        }

        public static bool IsIntInArray(int[] haystack, int needle)
        {
            for (int x = 0; x < haystack.Length; x++)
            {
                if (haystack[x] == needle)
                    return true;
            }
            return false;
        }
    }
}
